package mybatis.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import mybatis.Test;
import mybatis.controller.data.StudentRequest;
import mybatis.entity.Student;
import mybatis.service.IMybatisService;

@RestController
@RequestMapping(value = "/student")
public class MybatisController {
    @Autowired
    private IMybatisService mybatisService;

    @RequestMapping(value = "/getList")
    public String getStudentList() {
        return JSON.toJSONString(mybatisService.getStudents());
    }

    @RequestMapping(value = "/update4cache")
    public String update4cache(@RequestBody StudentRequest studentRequest) {
        mybatisService.update4cache(studentRequest.getId(), studentRequest.getName());
        return "update4cache";
    }

    @RequestMapping(value = "/queryInfo4cache", method = { RequestMethod.POST, RequestMethod.GET })
    public String queryInfo4cache(@RequestParam Long id) {
        return JSON.toJSONString(mybatisService.queryInfo4cache(id));
    }

    @RequestMapping(value = "/queryCollection/{id1}/{id2}", method = { RequestMethod.POST, RequestMethod.GET })
    public String queryCollection(@PathVariable(value = "id1") Long id1, @PathVariable(value = "id2") Long id2) {
        List<Long> list = new ArrayList<>();
        list.add(id1);
        list.add(id2);
        Long[] array = new Long[2];
        return JSON.toJSONString(mybatisService.queryCollection(list.toArray(array)));
    }

    @RequestMapping(value = "/queryCollection", method = { RequestMethod.POST })
    public List<Student> queryCollection1(@RequestBody StudentRequest studentRequest) {
        Long[] array = new Long[2];
        String sql = new SQL().INSERT_INTO("PERSON").VALUES("ID, FIRST_NAME", "${id}, ${firstName}")
                .VALUES("LAST_NAME", "${lastName}").toString();

        return mybatisService.queryCollection(studentRequest.getIds().toArray(array));
    }

    public static void main(String[] args) {
        // new SQL() {
        // {
        // DELETE_FROM("PERSON");
        // WHERE("ID = ${id}");
        // }
        // }.toString();

        new Test("use!!!!!") {
            {
                System.out.println(inner());
            }
        };
    }
}
